set -ex

#####################################
# PREPARE variables:
#
# NOTE that "app"/Contents specify slash. 
# this is not a problem even if app itself have slash at end.
# We will get something as  gg.app//Contents and this still works fine.
#
app="$BUILT_PRODUCTS_DIR/$PRODUCT_NAME.app/"
app_exe="$app"/Contents/MacOS/$PRODUCT_NAME

sys_vcomp=/usr/local/lib/vcomponents_x64
app_vcomp="$app"/Contents/Frameworks/vcomponents_x64
exec_vcomp="@executable_path/../Frameworks/vcomponents_x64"

app_frmw="$app"/Contents/Frameworks
app_resources="$app/Contents/Resources"



#####################################
# COPY files of vcomponents into APP.
#####################################
if [ ! -d "$app_vcomp" ] ; then
	mkdir "$app_vcomp"
fi

# VSHARED
if [ ! -e "$app_vcomp"/libvshared_fat_release_x64.dylib ] ; then
	cp $sys_vcomp/libvshared_fat_release_x64.dylib "$app_vcomp"
fi

# VKERNEL
if [ ! -e "$app_vcomp"/libvkernel_fat_release_x64.dylib ] ; then
	cp $sys_vcomp/libvkernel_fat_release_x64.dylib "$app_vcomp"
fi

# VCLIENT
if [ ! -e "$app_vcomp"/libvclient_fat_release_x64.dylib ] ; then
	cp $sys_vcomp/libvclient_fat_release_x64.dylib "$app_vcomp"
fi

# VREPORT
if [ ! -e "$app_vcomp"/libvreport_fat_release_x64.dylib ] ; then
	cp $sys_vcomp/libvreport_fat_release_x64.dylib "$app_vcomp"
fi


####################################
# Copy Folders:
####################################

# vresources
cp -R $sys_vcomp/vresources "$app_resources"



#####################################
# COPY /Library/Frameworks/V4CC_x64.framework into app/Frameworks
#####################################
if [ ! -d "$app_frmw" ] ; then
	mkdir "$app_frmw"
fi

cp -R /Library/Frameworks/V4CC_x64.framework "$app_frmw"


#####################################
# CHANGE executable_path for VCOMPONENTS dylibs:
#####################################

# change id of VSHARED
	install_name_tool -id "$exec_vcomp/libvshared_fat_release_x64.dylib"                                                    "$app_vcomp"/libvshared_fat_release_x64.dylib

# change id of VKERNEL
	install_name_tool -id "$exec_vcomp/libvkernel_fat_release_x64.dylib"                                                    "$app_vcomp"/libvkernel_fat_release_x64.dylib

	install_name_tool -change "$sys_vcomp/libvshared_fat_release_x64.dylib" "$exec_vcomp/libvshared_fat_release_x64.dylib" 	"$app_vcomp"/libvkernel_fat_release_x64.dylib
	install_name_tool -change "$sys_vcomp/VSDK.framework/Versions/A/VSDK" 	"$exec_vcomp/VSDK.framework/Versions/A/VSDK"   	"$app_vcomp"/libvkernel_fat_release_x64.dylib

# change id of VCLIENT
	install_name_tool -id "$exec_vcomp/libvclient_fat_release_x64.dylib"                                                    "$app_vcomp"/libvclient_fat_release_x64.dylib
	install_name_tool -change "$sys_vcomp/libvshared_fat_release_x64.dylib" "$exec_vcomp/libvshared_fat_release_x64.dylib"  "$app_vcomp"/libvclient_fat_release_x64.dylib

# change id of VREPORT
	install_name_tool -id "$exec_vcomp/libvreport_fat_release_x64.dylib"                                                    "$app_vcomp"/libvreport_fat_release_x64.dylib
	install_name_tool -change "$sys_vcomp/libvshared_fat_release_x64.dylib" "$exec_vcomp/libvshared_fat_release_x64.dylib"  "$app_vcomp"/libvreport_fat_release_x64.dylib
	install_name_tool -change "$sys_vcomp/libvkernel_fat_release_x64.dylib" "$exec_vcomp/libvkernel_fat_release_x64.dylib"  "$app_vcomp"/libvreport_fat_release_x64.dylib
	install_name_tool -change "$sys_vcomp/libvclient_fat_release_x64.dylib" "$exec_vcomp/libvclient_fat_release_x64.dylib"  "$app_vcomp"/libvreport_fat_release_x64.dylib



#####################################
# CHANGE executable_path for V4CC_x64.framework dylibs:
#####################################
app_frmw_v4cc="$app_frmw"/V4CC_x64.framework/Versions/A/V4CC_x64
 
 	install_name_tool -id "@executable_path/../Frameworks/V4CC_x64.framework/Version/A/V4CC_x64"                             "$app_frmw_v4cc"
  
	install_name_tool -change "$sys_vcomp/libvshared_fat_release_x64.dylib"   "$exec_vcomp/libvshared_fat_release_x64.dylib" "$app_frmw_v4cc"
	install_name_tool -change "$sys_vcomp/libvkernel_fat_release_x64.dylib"   "$exec_vcomp/libvkernel_fat_release_x64.dylib" "$app_frmw_v4cc"
	install_name_tool -change "$sys_vcomp/libvclient_fat_release_x64.dylib"   "$exec_vcomp/libvclient_fat_release_x64.dylib" "$app_frmw_v4cc"
	install_name_tool -change "$sys_vcomp/libvreport_fat_release_x64.dylib"   "$exec_vcomp/libvreport_fat_release_x64.dylib" "$app_frmw_v4cc"
    

#####################################
# now we need to update yet AppName.app/Contents/MacOS/AppName executable:
#####################################

	install_name_tool -change "/Library/Frameworks/V4CC_x64.framework/Versions/A/V4CC_x64"   "@executable_path/../Frameworks/V4CC_x64.framework/Versions/A/V4CC_x64" "$app_exe"
